package com.zhonggong.emp.activemq;

import java.util.ArrayList;
import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

import com.zhonggong.empProducer.service.ProducerManager;
import com.zhonggong.empProducer.service.impl.ProducerManagerImpl;
import com.zhonggong.network.emp.model.Emp;

public class EmpProducer {

	public List<Emp> empsList=new ArrayList<Emp>();
	public ProducerManager producerManager=new ProducerManagerImpl();
	private String userName = "root";
	private String password = "root";
	private String brokerURL = "tcp://0.0.0.0:61616";
	private ConnectionFactory factory;
	private Connection connection;
	private Session session;
	private Destination destination;
	private MessageProducer producer;
	
	
	public void start() {
		try {
			factory = new ActiveMQConnectionFactory(userName, password, brokerURL);
			
			connection = factory.createConnection();
		
		    connection.start();
		  
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			
			destination = session.createQueue("computers");
			
			producer = session.createProducer(destination);
			producer.setDeliveryMode(DeliveryMode.PERSISTENT);
			empsList=producerManager.queryAllEmp();
			String message=producerManager.json(empsList);
			
			TextMessage computerMessage=session.createTextMessage(message);
			producer.send(computerMessage);
			producer.close();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args) {
		
		EmpProducer producer=new EmpProducer();
		producer.start();
		
	}
}